layui.use('jquery', function () {
    $ = layui.jquery;
})

//弹出一个页面，title="编辑" url="/edit?id=1"
function openPage(title, url) {
    layer.open({
        title: title,
        type: 2,
        shade: 0.2,
        maxmin: true,
        shadeClose: true,
        area: ['100%', '100%'],
        content: url
    });
}

//在任意页面中可以刷新当前页面；
function refresh() {
    $(".fa-refresh", top.window.document).click()
}

//封装了一个post
function post(url, data) {
    let loading;let response;
    $.ajax({
        type: 'POST',
        async: false,
        dataType: 'json',
        url: url,
        beforeSend: function () {
            loading = layer.load(1, {shade: [0.1, '#fff']});
        },
        data: data,
        success: function (res) {
            layer.close(loading);
            response = res;
        },
        error: function () {
            layer.close(loading);
            let error = {
                code: -2,
                msg: "服务器响应错误"
            }
            response = error;
        }
    });
    return response;
}

/**
 * 获取几个工作日后的日期
 * @param date 参照日期
 * @param day  相隔天数
 * @returns {string}
 */
function getNextDate(date,day) {
    var dd = new Date(date);
    while (day>0){
        dd.setDate(dd.getDate() + 1);
        if(dd.getDay()>=1&&dd.getDay()<=5)
            day--;
    }
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
    return y + "-" + m + "-" + d;
};
//获取指定工作小时之后的时间
function getNextHour(date,hour) {
    var dd = new Date(date).getTime();
    hour=hour*60;
    while (hour>0){
        dd+=60*1000;
        if(new Date(dd).getHours()>=8&&new Date(dd).getHours()<17)
            hour--;
    }
   return new Date(dd);
};

//操作storage
Storage.set = function(name, val) {
    localStorage.setItem(name, val);
}

Storage.get = function(name) {
    return localStorage.getItem(name);
}

//打印内容的方法
function Print() {
    try {
        var shell = new ActiveXObject("WScript.Shell");
        //修改注册表，打印的时候去掉页脚的url
        shell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\footer", "&b&d", "REG_SZ");
    } catch (e) {
        console.log("error:" + e);
    }
    bdhtml = window.document.body.innerHTML;
    sprnstr = "<!--startprint-->";
    eprnstr = "<!--endprint-->";
    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML = prnhtml;
    window.print();
}
